package ee.karileet.productcatalogue.facade.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ee.karileet.productcatalogue.facade.IClientFacade;
import ee.karileet.productcatalogue.model.*;
import ee.karileet.productcatalogue.service.*;
import ee.karileet.productcatalogue.service.impl.*;

/**
 * @author Markus Karileet
 *
 */
public class ClientFacade implements IClientFacade {
	
	/**
	 * Logger variable
	 */
	private static final Log log = LogFactory.getLog(ClientFacade.class);
	private final IGroup gSrv = new GroupSrv();  
	private final IManufactor mSrv = new ManufactorSrv();
	private final IProduct pSrv = new ProductSrv();
	private final IUser uSrv = new UserSrv();
	
	@Override
	public Group getGroupDetails(int groupId) {
		log.debug("Returning group details for ID: " + groupId);
		Group group = null;
		try {
			group = gSrv.getGroup(groupId);
		} catch (Exception e) {
			log.error("Unable to get! " + e.getMessage());
		} 
		return group;
	}

	@Override
	public Manufactor getManufactorDetails(int manufactorId) {
		log.debug("Returning manufactor details for ID: " + manufactorId);
		Manufactor manufactor = null;
		try {
			manufactor = mSrv.getManufactor(manufactorId);
		} catch (Exception e) {
			log.error("Unable to get! " + e.getMessage());
		} 
		return manufactor;
	}

	@Override
	public Product getProductDetails(int productId) {
		log.debug("Returning product details for ID: " + productId);
		Product product = null;
		try {
			product = pSrv.getProduct(productId);
		} catch (Exception e) {
			log.error("Unable to get! " + e.getMessage());
		} 
		return product;
	}

	@Override
	public User getUserDetails(int userId) {
		log.debug("Returning product details for ID: " + userId);
		User user = null;
		try {
			user = uSrv.getUser(userId);
		} catch (Exception e) {
			log.error("Unable to get! " + e.getMessage());
		} 
		return user;
	}

	@Override
	public java.util.Set<Group> listGroups() {
		log.debug("Returning all groups");
		return gSrv.listGroups();
	}

	@Override
	public java.util.Set<Manufactor> listManufactors() {
		log.debug("Returning all manufactors");
		return mSrv.listManufactors();
	}

	@Override
	public java.util.Set<Product> listProductsByGroup(int groupId) {
		log.debug("Returning products of group ID: " + groupId);
		java.util.Set<Product> set = new java.util.HashSet<Product>(0);
		try {
			set = pSrv.listGroupProducts(groupId);
		} catch (Exception e) {
			log.error("Unable to list! " + e.getMessage());
		}
		return set;
	}

	@Override
	public java.util.Set<Product> listProductsByManufactor(int manufactorId) {
		log.debug("Returning products of manufactor ID: " + manufactorId);
		java.util.Set<Product> set = new java.util.HashSet<Product>(0);
		try {
			set = pSrv.listManufactorProducts(manufactorId);
		} catch (Exception e) {
			log.error("Unable to list! " + e.getMessage());
		}
		return set;
	}

	@Override
	public boolean register(String name, String password) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeUser(int userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateUser(int userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public java.util.HashMap<Manufactor, java.util.Set<Product>> listProductsByGroupWithManufactor(
			int groupId) {
		log.debug("Returning group products ordered by manufactor from group: " + groupId);
		java.util.HashMap<Manufactor, java.util.Set<Product>> set = new java.util.HashMap<Manufactor, java.util.Set<Product>>();
		try {
			set = pSrv.listGroupProductsByManufactor(groupId);
		} catch (Exception e) {
			log.error("Unable to list! " + e.getMessage());
		}
		return set;
	}

}
